<?php
namespace Admin\Controller;

class ZhanghaoController extends BaseController {
	public function index(){
		$res = $list = array();
    	$keywords = I('keywords');
    	$is_admin=session();

        $id=$is_admin['admin_id'];
        $roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();
        $roleid=$roleid['role_id'];
    	if(empty($keywords)){
           
    		$res = D('seller')->field('ty_users.nickname,ty_seller.*')->join('ty_users on ty_seller.user_id= ty_users.user_id')->where("ty_seller.group_id in(9,10) and is_admin=0 and ty_seller.roleid=$roleid " )->select();
    	}else{

    		$res = D()->query("select * from __PREFIX__seller where group_id in(9,10) and seller_name like '%$keywords%' and roleid=$roleid  order by seller_id");
    	}
    	$group = D('seller_group')->getField('group_id,group_name');

    	if($res && $group){
    		foreach ($res as $val){
    			$val['role'] =  $group[$val['group_id']];
    			$val['enabled'] = $val['enabled']>0 ? '启用' : '停用';
    			$val['add_time'] = date('Y-m-d H:i:s',$val['add_time']);
    			$list[] = $val;
    		}
    	}
        //var_dump($list);
    	$this->assign('list',$list);
        $this->display();
		
	}
    
    public function admin_info(){
    	$seller_id = I('get.seller_id');
        $is_admin=session('seller');
        $is_admin=$is_admin['is_admin'];
    	if($seller_id>0){
    		$info = D('seller')->where("seller_id=$seller_id")->find();

            $shangji_id=$info['shangji_id'];

            $store_id=$info['store_id'];
            $group_id = D('seller')->where("seller_id=$shangji_id")->find();
           if($group_id==''){
               $group_id='0';
            $sql="select b.nickname ,a.seller_id from ty_seller a, ty_users b  where  a.store_id=$store_id and a.user_id=b.user_id";
            $Model = M();
            $list = $Model->query($sql);
            $this->assign('list',$list);
           }else{
                $group_id=$group_id['group_id'];
            $sql="select b.nickname ,a.seller_id from ty_seller a, ty_users b  where a.group_id=$group_id and a.store_id=$store_id and a.user_id=b.user_id";
            $Model = M();
            $list = $Model->query($sql);
            $this->assign('list',$list);

           }
           
            
    		if($info){
    			$user = M('users')->where("user_id=".$info['user_id'])->find();

    		}
    		$info['user_name'] = empty($user['mobile']) ? $user['email'] : $user['mobile'];
    		$this->assign('info',$info);
    	}
        $res = D('seller')->field('ty_users.nickname,ty_seller.seller_id')->join('ty_users on ty_seller.user_id= ty_users.user_id')->where("store_id=".STORE_ID)->select();
        $role = D('seller_group')->where("store_id='1'")->select();
    	$this->assign('role',$role);
    	$this->assign('res',$res);
        $this->assign('is_admin',$is_admin);
        $this->display();
    }
    public function adminHandle(){
    	$data = I('post.');
        
        $group_info   =  M('seller_group')->where(array('group_id'=>$data['group_id']))->find();
        $erp_roleid   = array_search($group_info['group_name'],C('group_seller'));

    	if($_GET['act']=='del' && $_GET['seller_id']>0){
    		//删除店铺管理员
    		$manage = M('seller')->where(array('seller_id'=>$_GET['seller_id']))->find();
            $roleid = M('admin')->where(array('admin_id'=>session('admin_id')))->getField('role_id');

    		if($manage['roleid'] == $roleid){   
    			$r = M('seller')->where('seller_id='.$_GET['seller_id'])->delete();
    			//sellerLog('删除店铺管理员'.$manage['seller_name']);
                //向erp传送数据
                $url = C('ERP_URL').'/right/deleteuser';
                $request = httpRequest($url,'POST',encryptDecrypt(C('KEY_ERP'),array('username'=>$manage['seller_name']),0));
    		}else{
    			exit(json_encode(0));//只能删除本店的管理员
    		}
    		exit(json_encode(1));
    	}  
    	
    	if($data['seller_id']>0){
    		$seller = session('seller');//修改密码

    	    $seller_info = M('seller')->join('ty_users ON ty_users.user_id = ty_seller.user_id')->join('ty_seller_group ON ty_seller.group_id = ty_seller_group.group_id')->where(array('ty_seller.seller_id'=>$data['seller_id'],'ty_seller.store_id'=>0))->field('ty_seller.seller_name AS name,ty_seller.roleid AS locationId,ty_seller.store_id AS per_id,ty_users.nickname AS username,ty_users.mobile,ty_seller_group.group_name AS roleId,ty_users.user_id')->find();

                if($data['password'] == '' && $data['password2'] == ''){
                    if($data['shangji_id']!=''){
                         $r = M('seller')->where(array('seller_id'=>$data['seller_id']))->save(array('shangji_id'=>$data['shangji_id']));
                    }
    			
                }else{
                    if($data['password'] == $data['password2']){
                        $this->error("两次密码一致",U('zhanghao/admin_info',array('seller_id'=>$seller['seller_id'])));
                    }else{  
                        if(M('users')->where(array('user_id'=>$seller_info['user_id'],'password'=>encrypt($data['password'])))->count()>0){
                            $r = M('users')->where(array('user_id'=>$seller_info['user_id']))->save(array('password'=>encrypt($data['password2'])));
                            $seller_info['userpwd'] =   $data['password2'];
                            $seller_info['roleId']  =   array_search($seller_info['roleid'],C('group_all'));
                            $seller_info['locationId']  =  $seller_info['locationid'];
                            //向erp传送数据
                            $url = C('ERP_URL').'right/updateuser';
                            $request = httpRequest($url,'POST',encryptDecrypt(C('KEY_ERP'),$seller_info,0));
                        }else{
                            $this->error("原密码错误",U('zhanghao/admin_info',array('seller_id'=>$seller['seller_id'])));
                        }
                    }
                }
    		
    	}else{
    		//验证商家后台登陆账号是否有同名	
    		if(M('seller')->where("seller_name='".$data['seller_name']."'")->count()){
    			$this->error("此登陆账号名已被注册，请更换",U('zhanghao/admin_info'));
    		}
    		$uname = check_email($data['user_name']) ? 'email' : 'mobile';

    		//查找验证绑定用户
    		$userinfo = M('users')->where("$uname ='".$data['user_name']."'")->find();

    		if(empty($userinfo)){
    			$this->error("请先注册前台会员",U('zhanghao/admin_info'));
    		}elseif($userinfo['password'] != encrypt($data['password'])){
    			$this->error("登陆密码错误",U('zhanghao/admin_info'));
    		}else{
    			if(M('seller')->where("user_id=".$userinfo['user_id'])->count()){
    				$this->error("该用户已经添加过店铺管理员",U('zhanghao/admin_info'));
    			}
    			$is_admin=session();
        		$id=$is_admin['admin_id'];
        		$roleid=M('admin')->field('role_id')->where("admin_id=$id")->find();
        		$roleid=$roleid['role_id'];
    			$data['password'] = encrypt($data['password']);
    			$data['user_id'] = $userinfo['user_id'];
    			$data['store_id'] = STORE_ID;
    			$data['add_time'] = time();
    			$data['roleid']=$roleid;
    			unset($data['seller_id']);
    			$r = M('seller')->add($data);
                //向erp传送数据
                $url = C('ERP_URL').'right/adduser';
                $request = httpRequest($url,'POST',encryptDecrypt(C('KEY_ERP'),array('username'=>$data['seller_name'],'locationId'=>$r,'userpwd'=>$data['password'],'name'=>$data['user_name'],'mobile'=>$data['seller_name'],'roleId'=>$erp_roleid,'per_id'=>0),0));

                $a= M('users')->where(array('user_id'=>
                    $data['user_id']))->save(array('group_id'=>$data['group_id']));
    		}
    	}
    	if($r!==false){
    		$this->success("操作成功",U('zhanghao/index'));
    	}else{
    		$this->error("操作失败",U('zhanghao/index'));
    	}
    }

}
?>